# -*- coding: utf-8 -*-
from util.utils import Utils
from util.constants import Constants
from user.kangmaUser import KangmaUser
from pprint import pprint


########################################################################
# 订单模块
########################################################################

class KangmaOrder():

    def __init__(self, user):
        self.user = user

    ################################################### APP ###############################################################

    # 商品加入进货单
    def productIntoPurchase(self,
                            args={}
                            ):
        url = Constants.APP_PRODUCT_INTO_PURCHASE_URL
        data = {
            'skuId': args['skuId'],  # skuId
            'quantity': args['quantity'],  # 数量
            'mealId': args['mealId'],  # 套餐ID
            'fullGiftId': args['fullGiftId'],  # 满赠活动ID
            'isGift': args['isGift'],  # 是否赠品  0 false 非赠品 1 true 是赠品
            'giftPrice': args['giftPrice']  # 赠品单价
        }
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            data=data
        )
        # 获取code
        code = Utils.getReturnCode(r)
        if code:
            pprint('【商品加入进货单】 接口返回异常')
            return -1

        if code == 0:
            pprint('【商品加入进货单】 操作成功')
            return 0
        else:
            pprint('【商品加入进货单】 接口返回异常')
            return -1

    # 进货单列表
    def productIntoPurchaseList(self):
        url = Constants.APP_PRODUCT_INTO_PURCHASE_LIST_URL
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            {}
        )
        return Utils.getReturnData(r)

    # 进货单数量
    def productIntoPurchaseCount(self):
        url = Constants.APP_PRODUCT_INTO_PURCHASE_COUNT_URL
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            {}
        )
        return Utils.getReturnData(r)

    # 确认下单页面
    def confirmOrderList(self):
        url = Constants.APP_CONFIRM_ORDER_LIST_URL
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            {}
        )
        return Utils.getReturnData(r)

    # 立即购买/确认下单页面/预团购实时获取返利金额
    def getRebateDiscount(self, args={}):
        url = Constants.APP_GET_REBATE_DISCOUNT_URL
        data = {
            'sourceType': args['sourceType'],  # 来源 0-立即购买 1-购物车 2 预团购
            'skuId': args['skuId'],  # 商品SKU的ID 立即购买，预团购必传
            'quantity': args['quantity'],  # 商品数量 立即购买，预团购必传
            'supplierId': args['supplierId'],  # 供应商id 立即购买，预团购必传
            'supplierDetail': args['supplierDetail'],  # 商家商品信息
            'groupPurchaseId': args['groupPurchaseId'],  # 预团购活动ID，预团购必传
            'receiverId': args['receiverId']  # 收货人信息
        }
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            data=data
        )
        return Utils.getReturnData(r)

    # 提交订单
    def submitOrder(self, args={}):
        url = Constants.APP_SUBMIT_ORDER_URL
        data = {
            'sourceType': args['sourceType'],  # 来源 0-立即购买 1-购物车 2 预团购
            'skuId': args['skuId'],  # 商品SKU的ID 立即购买，预团购必传
            'quantity': args['quantity'],  # 商品数量 立即购买，预团购必传
            'supplierId': args['supplierId'],  # 供应商id 立即购买，预团购必传
            'paymentTypeId': args['paymentTypeId'],  # 支付方式Id
            'receiverId': args['receiverId'],  # 收货人信息
            'invoiceType': args['invoiceType'],  # 发票类型 0:不开发票 1:增值税普通发票 2:增值税专用发票
            'invoiceTitle': args['invoiceTitle'],  # 发票抬头
            'invoiceTaxNumber': args['invoiceTaxNumber'],  # 发票税号
            'invoiceContent': args['invoiceContent'],  # 发票内容
            'invoiceMobile': args['invoiceMobile'],  # 收票人手机号
            'invoiceBank': args['invoiceBank'],  # 开户行
            'invoiceBankCard': args['invoiceBankCard'],  # 银行卡号
            'supplierDetail': args['supplierDetail'],  # 商家商品信息
            'orderPlatform': args['orderPlatform'],  # 下单平台 0 康药多 1 康药麦麦
            'groupPurchaseId': args['groupPurchaseId']  # 预团购活动ID，预团购必传
        }
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            data=data
        )
        return Utils.getReturnData(r)

    # 提交付款单
    def submitPayment(self, args={}):
        url = Constants.APP_CONFIRM_PAY_ORDER_URL
        data = {
            'paymentMethodId': args['paymentMethodId'],  # 支付方式id
            'sn': args['sn'],  # 付款单编号
            'orderPlatform': args['orderPlatform']  # 下单平台 0 康药多 1 康药麦麦
        }
        r = Utils.post(
            self.user.contentType,
            self.user.clientId,
            self.user.clientSecret,
            self.user.authorization,
            url,
            data=data
        )
        return Utils.getReturnData(r)


################################################### APP ###############################################################


if __name__ == '__main__':
    pass
